﻿hpxAdminApp.controller('commissionController', function ($rootScope, $scope, $stateParams, $state, API_URL, NgTableParams, commissionService) {
    $scope.filter = {
        enterpriseName: '',
        rechargeAmount: 0,
        rechargeDraftBean: 0,
        conversionRule: 1,
        defaultOption:1,
        ratios: [
            {
                lable: '1:1',
                value: 1
            },
            {
                lable: '1:1.1',
                value: 1.1
            },
            {
                lable: '1:1.2',
                value: 1.2
            },
            {
                lable: '1:1.3',
                value: 1.3
            },
            {
                lable: '1:1.4',
                value: 1.4
            },
            {
                lable: '1:1.5',
                value: 1.5
            }
        ]
    };
    var emptyEntity = {};

    var newEntity = angular.copy(emptyEntity, newEntity);
    //初始化列表
    $scope.tableParams = new NgTableParams({ sorting: { 'update_time': 'desc' } }, {
        getData: function (params) {
            return commissionService.getBeansList(params, $scope.filter.enterpriseName).then(function (data) {
                $scope.first = $scope.getFirst(params);
                if (data) {
                    return data.items
                }
            });
        }
    });

    $scope.refresh = function () {
        $scope.tableParams.reload();
    };

    $scope.changeAmount = function () {
        if ($scope.filter.rechargeAmount == '' || $scope.filter.rechargeAmount == 0 || $scope.filter.rechargeAmount == undefined) {
            $scope.filter.rechargeDraftBean = 0
        } else {
            $scope.filter.rechargeDraftBean = parseInt($scope.filter.defaultOption * $scope.filter.rechargeAmount * 1)
        }
    }

    $scope.changeRatio = function () {
        if ($scope.filter.defaultOption) {
            switch ($scope.filter.defaultOption) {
                case 1:
                    $scope.filter.conversionRule = 1
                    break;
                case 1.1:
                    $scope.filter.conversionRule = 2
                    break;
                case 1.2:
                    $scope.filter.conversionRule = 3
                    break;
                case 1.3:
                    $scope.filter.conversionRule = 4
                    break;
                case 1.4:
                    $scope.filter.conversionRule = 5
                    break;
                case 1.5:
                    $scope.filter.conversionRule = 6
                    break;
            }
            $scope.filter.rechargeDraftBean = parseInt($scope.filter.defaultOption * $scope.filter.rechargeAmount * 1)
        }
    }

    $scope.edit = function (data) {
        if (data == null) {
            $scope.model = newEntity;
        }
        else {
            $scope.model = angular.copy(data);
        }
        $('#modal-edit').modal('show');
    };
    $scope.save = function (model) {
        commissionService.update(model.enterpriseId, $scope.filter).then(function () {
            $scope.tableParams.reload();
            $scope.editForm.$setPristine();
            $('#modal-edit').modal('hide');
        });
    };
});
